package com.example.module_splash

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import androidx.core.animation.addListener
import androidx.core.view.isVisible
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.lib_base.BaseActivity
import com.example.module_splash.databinding.ActivitySplashBinding
import com.youth.banner.loader.ImageLoader

class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    private val bannerList= listOf("https://ts1.cn.mm.bing.net/th?id=OIP-C.Zte3ljd4g6kqrWWyg-8fhAHaEo&w=316&h=197&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2","https://ts2.cn.mm.bing.net/th?id=OIP-C.mH9YLFEL5YdVxJM82mjVJQAAAA&w=316&h=197&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2","https://ts2.cn.mm.bing.net/th?id=OIP-C.7sAjIeoQYWnXV_QnuYs1jQHaEK&w=333&h=187&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2")
    override fun initData() {

    }

    override fun initView() {
     binding.splashBanner.update(bannerList)
        binding.splashBanner.setImageLoader(object : ImageLoader(){
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                Glide.with(imageView!!).load(path as String).into(imageView)
            }

        }).start()
        binding.splashBanner.setOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                if(position==2){
                    binding.splashBanner.stopAutoPlay()
                    binding.time.isVisible=true
                    val ofInt = ValueAnimator.ofInt(3, 0)
                    ofInt.duration=3000
                    ofInt.interpolator=LinearInterpolator()
                    ofInt.addUpdateListener {
                        val index=it.animatedValue as Int
                        if(index!=-1){
                            binding.time.text="倒计时:${index}"
                        }
                    }
                    ofInt.addListener(object :AnimatorListenerAdapter(){
                        override fun onAnimationEnd(animation: Animator) {
                            super.onAnimationEnd(animation)
                            ARouter.getInstance().build("/sp/jump").navigation()
                            finish()
                        }
                    })
                    ofInt.start()
                }
            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })

    }

    override fun getLayoutId(): Int =R.layout.activity_splash

}